// 树结构初始化加载
var setting = {
    check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
    view: { selectedMulti: false, nameIsHTML: true },
    data: { simpleData: { enable: true, idKey: "Id", pIdKey: "Pid", }, key: { name: "ActionMethodName", title: "ActionMethodName" } },
    callback: {
        beforeClick: function (treeId, treeNode, clickFlag) {
            var menuTrees = $.fn.zTree.getZTreeObj(treeId);
            menuTrees.checkNode(treeNode, !treeNode.checked, true, true);
            return false;
        }
    }
}, menuTrees, loadTree = function () {
    $.get("/Menus/AllMenuData", function (data) {
        menuTrees = $.fn.zTree.init($("#menuTrees"), setting, data); //.expandAll(true);
    }, null, null, "正在加载，请稍后...");
}; loadTree();

$("#form-role-add").validate({
    rules: {
        RoleName: {
            required: true
        },
        Modifdon: {
            required: true,
        },
        Sort: {
            required: true,
        },
    },
    messages: {
        "roleName": {
            remote: "角色已经存在"
        }
    },
    submitHandler: function (form) {
        add();
    }
});

function getCheckeds() {
    var menuIds = "";
    var treeNodes = menuTrees.getCheckedNodes(true);
    for (var i = 0; i < treeNodes.length; i++) {
        if (0 == i) {
            menuIds = treeNodes[i].Id;
        } else {
            menuIds += ("," + treeNodes[i].Id);
        }
    }
    return menuIds;
}

function add() {
    var roleName = $("input[name='RoleName']").val();
    var roleKey = $("input[name='Modifdon']").val();
    var roleSort = $("input[name='Sort']").val();
    var status = $("input[name='DelFlag']").is(':checked') == true ? 0 : 1;
    var remark = $("input[name='Remark']").val();
    var menuIds = getCheckeds();

    //var data = {
    //    "roleName": roleName,
    //    "roleKey": roleKey,
    //    "roleSort": roleSort,
    //    "status": status,
    //    "remark": remark,
    //    "menuIds": menuIds
    //}
    //alert(JSON.stringify(data));
    //return false;

    $.ajax({
        cache: true,
        type: "POST",
        url: "/Roles/AddRoleData",
        data: {
            "roleName": roleName,
            "roleKey": roleKey,
            "roleSort": roleSort,
            "status": status,
            "remark": remark,
            "menuIds": menuIds
        },
        async: false,
        error: function (request) {
            $.modalAlert("系统错误", modal_status.FAIL);
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("新增成功,正在刷新数据请稍后……", { icon: 1, time: 500, shade: [0.1, '#fff'] }, function () {
                    $.parentReload();
                });
            } else {
                $.modalAlert(data.msg, modal_status.FAIL);
            }

        }
    });
}
